package Dragons;

import java.util.ArrayList;

import jgame.JGPoint;
import jgame.JGTimer;
import Main.Game;
import Other.Field;
import Units.Dragon;
import Units.UnitName;

public class Dragon4 extends Dragon {
	public static int imageHeight = 86, imageWidth = 74;
	
	private final static String stringStandingDown = "dragon4",
								stringGoDown	   = "robot1animgo_down",
								stringStopDown	   = "robot1animstop_down",
								stringMoveDown	   = "robot1animmove_down";
	
	public Dragon4(String name, boolean unique, double posX, double posY, int cid){
		super(name, unique, posX, posY, cid, stringStandingDown);
	}

	/*@Override
	public boolean canMove(Field field) {
		if (getSteps() != 2) return false;
		if (!Game.board.inBoard(field.i, field.j)) return false;
		boolean ret = false;
		
		for (int dir = 0; dir < 6; dir ++){
			int di = Field.adjacentFields[dir][0];
			int dj = Field.adjacentFields[dir][1];
			
			Field current = new Field(this.getField());
			while (true){
				current.i += di;
				current.j += dj;
				if (!Game.board.inBoard(current.i, current.j)) break;
				if ( UnitName.isDragon(Game.board.getUnitName(current.i, current.j)) ) break;
				
				if (current.i == field.i && current.j == field.j) {
					ret = true;
					break;
				}
				if ( Game.board.getUnitName(current.i, current.j) != UnitName.EMPTY ) break;
			}
		}
		
		return ret;
	}*/
	
	@Override
	public boolean canMove(Field field) {
		if (!Game.board.inBoard(field.i, field.j)) return false;
		if ( UnitName.isDragon(Game.board.getUnitName(field.i, field.j)) ) return false;

		if (true) {
			for (int dir = 0; dir < 6; dir++){
				Field current = new Field(this.getField());
				
				if (current.compare(field)){
					return true;
				}
				
				int di = Field.adjacentFields[dir][0];
				int dj = Field.adjacentFields[dir][1];
				
				current.i += di; current.j += dj;
				if (current.compare(field)){
					return true;
				}
				
				current.i += di; current.j += dj;
				if (current.compare(field)){
					return true;
				}
				
				current.i -= di; current.j -= dj;
				
				di = Field.adjacentFields[(dir + 1) % 6][0];
				dj = Field.adjacentFields[(dir + 1) % 6][1];
				
				current.i += di; current.j += dj;
				
				if (current.i == field.i && current.j == field.j){
					return true;
				}
			}
		}
		
		return false;
	}
	
	@Override
	public ArrayList<Field> whereAttack(Field field) {
		
		ArrayList<Field> ret = new ArrayList<Field>();
		
		ret.clear();
		
		if (getSteps() != 1) return ret;
		if (!Game.board.inBoard(field.i, field.j)) return ret;
		if ( !UnitName.isRobot(Game.board.getUnitName(field.i, field.j)) ) return ret;

		for (int dir=0; dir<Field.adjacentFields.length; dir++) {
			int di = Field.adjacentFields[dir][0];
			int dj = Field.adjacentFields[dir][1];
			
			Field p1 = new Field(this.getField().i + di, this.getField().j + dj);
			if ( field.compare(p1) && UnitName.isDragon(Game.board.getUnitName(p1.i, p1.j)) ) ret.add(p1);
		}

		return ret;
	}
	
	@Override
	public boolean canAttack(Field field) {
		
		if (getSteps() != 1) return false;
		if (!Game.board.inBoard(field.i, field.j)) return false;
		if ( !UnitName.isRobot(Game.board.getUnitName(field.i, field.j)) ) return false;

		for (int dir=0; dir<Field.adjacentFields.length; dir++) {
			int di = Field.adjacentFields[dir][0];
			int dj = Field.adjacentFields[dir][1];
			
			Field p1 = new Field(this.getField().i + di, this.getField().j + dj);
			if ( field.compare(p1) && UnitName.isDragon(Game.board.getUnitName(p1.i, p1.j)) ) return true;
		}
		
		return false;
	}
	
	public void move() {
		switch (getState()){
			case 0:
				setGraphic(stringStandingDown);
				break;
			case 1:
				setGraphic(stringGoDown);
				break;
			case 2:
				setGraphic(stringMoveDown);
				JGPoint p = Game.board.getRealCoordinates(getTargetField());
				
				if (Game.board.distance(p, new JGPoint((int)getLastX() + this.imageWidth/2, (int)getLastY() + this.imageHeight/2)) < 5){
					xspeed = yspeed = 0;
					setState(3);
					new JGTimer(5, true){

						@Override
						public void alarm() {
							setState(0);
							Game.board.setAnimation(false);
							Field f = Dragon4.this.getField();
							Field t = Dragon4.this.getTargetField();
							
							Game.board.moveFromTo(f, t);
							Dragon4.this.setField(Dragon4.this.getTargetField());
						}						
					};
				}
				break;
			case 3:
				setGraphic(stringStopDown);
				break;
			default: setGraphic(stringStandingDown);
		}
	}
}
